package com.zhj.leetcode;

/**
 * 力扣74 搜索二维矩阵
 * @author zhj
 */
public class Test74 {
    public static void main(String[] args) {
        int[][] matrix= {{1,3,5,7},{10,11,16,20},{23,30,34,50}};
        System.out.println(search(matrix,23));
    }

    private static boolean search(int[][] matrix, int target) {
        if (matrix == null || matrix.length == 0) {
            return false;
        }
        int row = matrix.length;
        int col = matrix[0].length;
        int l = 0;
        int r = row * col - 1;
        while (l <= r) {
            int mid = l + (r - l) / 2;
            int element = matrix[mid/col][mid%col];
            if (element == target) {
                return true;
            } else if (element > target) {
                r = mid - 1;
            } else {
                l = mid + 1;
            }
        }
        return false;
    }
}
